//Celý mapa objekt
var myGoogleMap = {
    bounds: null,
    map: null
}


//init mapy
myGoogleMap.init = function(selector, latLng, zoom) {
    var myOptions = {
        zoom:zoom,
        center: latLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    //mapa je v div class map
    this.map = new google.maps.Map(document.getElementById("map"),
        myOptions);
    this.bounds = new google.maps.LatLngBounds();
}
//usadí na mapu markery
myGoogleMap.placeMarkers = function() {
    $.post("personServlet",{
        action: "setMarkers"
    }, function(xml){


        $(xml).find("marker").each(function(){

            // create a new LatLng point for the marker
            var name = $(this).find('name').text();
            var id = $(this).find('id').text();
            var lat = $(this).find('lat').text();
            var lon = $(this).find('lon').text();
            var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lon));

            myGoogleMap.bounds.extend(point);
            // přidá určitého markra
            var marker = new google.maps.Marker({
                position: point,
                map: myGoogleMap.map,
                title: name


            });
            marker.set('op',0);


            // posluchč na markra. Na klitnutí vysune infowindow
            //na druhé kliknutí se zatáhne
            google.maps.event.addListener(marker, 'click', function() {


                if (marker.get('op') == 0){
                    var html = null;

                    //infowindow.setContent(html);
                    /*$.post("personServ",{ action: "clickOnMarker", id : marker.get("id")},
                                function(res){
                                    html = res;
                                });*/
                    $.ajax({
                        type: 'POST',
                        url: "personServlet",
                        data: {
                            action: "clickOnMarker",
                            id : marker.get("id")
                            },
                        success: function(res){
                            html = res;
                        },
                        //dataType: "xml",

                        // asynchronita aby se obrázky natáhly celé
                        // a najednou.
                        async: false
                    });


                    var infowindow = new google.maps.InfoWindow({
                        content: html
                    });
                    infowindow.open(marker.map,marker);
                    marker.set('infowindow',infowindow);
                    marker.set('op',1);
                    //var testB = marker.get('infowindow');
                    //var testC = testB.getContent();
                    //var testF = infowindow.getMap();
                    //var testD = testC;

                }
                else if (marker.get('op') == 1){
                    var infowindow = marker.get('infowindow');
                    infowindow.close();
                    marker.set('op',0);

                }


            });
            marker.set("id", id);
        });
        // Fit the map around the markers we added:
        myGoogleMap.map.fitBounds(myGoogleMap.bounds);
    });
}
//Zacentruje mapu
$(document).ready(function() {
    $("#map").css({
        height: 700,
        width: 940
    });
    var myLatLng = new google.maps.LatLng(50.74033553, 14.25067267);
    myGoogleMap.init('#map', myLatLng, 11);
    myGoogleMap.placeMarkers();

});


